<?php

class Mailer {
    private $CI;

    public function __construct() {
        $this->CI =& get_instance();
    }

    public function send($data) {
        $this->CI->load->library('Mailer/PHPMailer');
        $this->CI->load->library('Mailer/SMTP');

        $this->CI->load->config('mailer');
        $config = $this->CI->config->item('mail');


        $search = array('ESTATE_TYPE','CITY','DISTRICT','AREA','PRICE','FULLNAME', 'PHONE', 'EMAIL', 'NOTE');
        $replace = array($data['estate_type'],$data['city'],$data['district'],$data['area'],$data['price'],$data['fullname'],$data['phone'],$data['email'],$data['note']);
        $body = file_get_contents(base_url('body.html'));

        $body = str_replace($search, $replace, $body);

        $mail = new PHPMailer();

        $mail->IsSMTP();
        $mail->Host = $config['host'];
        $mail->SMTPAuth = $config['SMTPAuth'];
        $mail->SMTPSecure = $config['SMTPSecure'];
        $mail->Port = $config['port'];
        $mail->CharSet = $config['CharSet'];
        $mail->Username = $config['email'];
        $mail->Password = $config['password'];
        $mail->SMTPDebug = FALSE;

        $mail->SetFrom($config['email'], $config['from']);
        $mail->AddAddress('kevinvn.1709@gmail.com');
        $subject = 'Test email';
        $mail->Subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
        $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";

        $mail->MsgHTML($body);
        if (!$mail->Send()) {
            // writeLog
            return TRUE;
        } else {
            // writeLog
            return FALSE;
        }
    }
}